
clear
set more off
program drop _all

local owndir = "" // put your path here

global dirdata = "`owndir'/Raw_Data"
global dirin = "`owndir'/Dta"
global dirout = "`owndir'/Output"


use "$dirin/flows_ij_by_age.dta", clear
collapse (sum) flow, by(destination all_dates)
gen month=month(all_dates)
gen day=day(all_dates)

// dropping holidays
drop if month==1&day==1
drop if month==1&day==24
drop if month==1&day==25
drop if month==1&day==26
drop if month==1&day==27
drop if month==4&day==15
drop if month==4&day==30
drop if month==5&day==1
drop if month==5&day==4
drop if month==5&day==5

gen dow=dow(all_dates)

drop if  month==1

gen flow1=flow if day==1 & month==2
gen flow2=flow if day==2 & month==2
gen flow3=flow if day==3 & month==2
gen flow4=flow if day==4 & month==2
gen flow5=flow if day==5 & month==2
gen flow6=flow if day==6 & month==2
gen flow7=flow if day==7 & month==2

bysort destination: egen min_dow6=min(flow1)
bysort destination: egen min_dow0=min(flow2)
bysort destination: egen min_dow1=min(flow3)
bysort destination: egen min_dow2=min(flow4)
bysort destination: egen min_dow3=min(flow5)
bysort destination: egen min_dow4=min(flow6)
bysort destination: egen min_dow5=min(flow7)

gen normalization=min_dow3 if dow==3
replace normalization=min_dow4 if dow==4
replace normalization=min_dow5 if dow==5
replace normalization=min_dow6 if dow==6
replace normalization=min_dow0 if dow==0
replace normalization=min_dow1 if dow==1
replace normalization=min_dow2 if dow==2

keep destination flow all_dates month dow normalization
gen flow_norm=(flow-normalization)/normalization*100

collapse (mean) flow_norm (median) median=flow_norm (p90) p90=flow_norm (p10) p10=flow_norm ///
(sd) sd=flow_norm (min) min=flow_norm (max) max=flow_norm, by(all_dates dow)

gen p95t=1.96*sd + flow_norm
gen p95b=-1.96*sd + flow_norm

#delimit
twoway 
(scatter flow_norm all_dates if dow!=6 & dow!=0, mstyle(p1) mcolor(black))
(rcap p10 p90 all_dates if dow!=6 & dow!=0, lstyle(ci))
, ylabel(-15 "-15%" -10 "-10%" -5 "-5%" 0 "0%") tlabel(10feb2020  10mar2020 10apr2020 10may2020, format(%d)) 
leg(order(1 "Mean" 2 "95 percent confidence interval") region(lwidth(none)) ring(0)) leg(off)
ytitle("pct change inflows to district relative to 1st wk Feb") xtitle(" ")
graphregion(color(white)) plotregion(fcolor(white));
#delimit cr	
graph export "$dirout/Figure1.png", replace

